﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcEngine.Mvc.StarterSite.Controllers;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Core;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Helpers;
using System.Web;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core.Configuration;
using MvcEngine.Mvc;
using MvcEngine.Mvc.Models;
using System.IO;
using MvcEngine.Gallery.Components.Extensions;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;
using MvcEngine.Gallery.Managers;
using MvcEngine.Gallery.Models;
using MvcEngine.Core.Localization;
using MvcEngine.Core.Base;

namespace MvcEngine.Gallery.StarterSite.Controllers
{
    public class GalleryImageController : BaseController
    {
        public MvcEngineModel CategoryFilter(string categoryName, Guid galleryId, string pageName, int? pageNumber)
        {
            if (!pageNumber.HasValue || pageNumber <= 0)
                pageNumber = 1;

            var gallery = GalleryManager.Current.GetGallery(galleryId);

            if (string.IsNullOrEmpty(categoryName) && gallery.DefaultCategoryId.HasValue)
            {
                var cat = GalleryCategoryManager.Current.Get(gallery.DefaultCategoryId.Value);
                categoryName = cat.Slug;
            }

            var category = GalleryCategoryManager.Current.Get(categoryName, galleryId);
            if (category != null)
            {
                LoadOptions options = IoC.Resolve<LoadOptions>();
                options.PageNumber = pageNumber.Value;
                options.PageSize = gallery.ImagesPerPage;
                options.SortAscending = true;

                IPagination<GalleryImage> pagination = GalleryImageManager.Current.GetImagesByCategory(category.Id, options);
                GalleryImageListPaginationViewModel model = new GalleryImageListPaginationViewModel()
                {
                    TotalItems = pagination.TotalItems,
                    TotalPages = pagination.TotalPages,
                    CurrentPage = pageNumber.Value,
                    GalleryName = gallery.Name,
                    PageName = pageName,
                    Description = category.Description,
                    Keywords = category.Keywords,
                    CategoryName = category.Name,
                    ViewName = "CategoryFilter",
                    Images = new List<GalleryImageDetailsViewModel>(),
                    IsCorrectPage = true
                };

                if (pageNumber.Value > pagination.TotalPages && pagination.TotalPages != 0)
                {
                    model.IsCorrectPage = false;
                }

                if (pagination.TotalItems == 0)
                    return model;

                foreach (var image in pagination)
                {
                    var imageViewModel = new GalleryImageDetailsViewModel()
                    {
                        Id = image.Id,
                        Categories = GalleryCategoryManager.Current.GetAllByImage(image.Id),
                        Name = image.Name,
                        Url = image.Url,
                        Type = image.Type,
                        Description = image.Description,
                        Visible = image.Visible
                    };
                    model.Images.Add(imageViewModel);
                }
                return model;
            }
            return new MvcEngineModel() { ViewName = "CategoryNotFound" };
        }
    }
}
